package cn.facetoken.homer;

import android.app.Activity;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.openbiotoken.android.UriBitmapHelper;

public class PhotoRegistrationRecyclerViewAdapter
        extends RecyclerView.Adapter<PhotoRegistrationViewHolder> {

    PhotoRegistrationRecyclerViewAdapter(@NonNull final Activity activity,
                                         @NonNull final List<PhotoRegistration<?>> photoRegistrationList) {
        this.activity = activity;
        this.photoRegistrationList = photoRegistrationList;
    }

    @NonNull
    private final Activity activity;

    @NonNull
    final List<PhotoRegistration<?>> photoRegistrationList;

    @NonNull
    @Override
    public PhotoRegistrationViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
        final View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.face_registration_item_list_content, parent, false);
        return new PhotoRegistrationViewHolder(activity, this, view);
    }

    @Override
    public void onBindViewHolder(@NonNull final PhotoRegistrationViewHolder holder, final int position) {
        @NonNull final PhotoRegistration<?> photoRegistration = photoRegistrationList.get(position);

        // Tags
        holder.itemView.setTag(position);
        photoRegistration.tagView(holder.itemView);

        // Setup sub views
        holder.nameView.setText(photoRegistration.name);
        @NonNull final UriBitmapHelper bitmapHelper = new UriBitmapHelper(activity, Uri.parse(photoRegistration.photoUri));
        holder.photoView.setImageBitmap(bitmapHelper.loadBitmap());
    }

    @Override
    public int getItemCount() {
        return photoRegistrationList.size();
    }
}
